<?php

	$wproot = dirname(dirname(dirname(dirname(__FILE__))));
	
	require_once($wproot . '/wp-load.php');
	require_once($wproot . '/wp-admin/includes/admin.php');
	
	if (is_user_logged_in() ) {
		if (isset($_GET['q'])) {

			$tax = get_taxonomy( 'post_tag' );			
			$s = stripslashes( $_GET['q'] );
			if ( false !== strpos( $s, ',' ) ) {
				$s = explode( ',', $s );
				$s = $s[count( $s ) - 1];
			}
			$s = trim( $s );
			if ( strlen( $s ) < 2 )
				die; // require 2 chars for matching						
			
			$results = $wpdb->get_results( $wpdb->prepare( "SELECT t.name, tt.description FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.name LIKE (%s) limit 0, 10", 'post_tag', '%' . like_escape( $s ) . '%' ) );
			
			echo json_encode($results);
			die;			
		}
		
	}
?>